import { _signin } from '../../common/api/signin'
const app = getApp()
Page({
  data: {
    name: '',
    password: '',
    disabled: false,
    signing: false
  },
  inputHandler (e) {
    let attr = e.currentTarget.dataset.attr
    this.data[attr] = e.detail
  },
  signin () {
    if (!this.data.name.trim()) {
      app.toast('请输入用户名')
      return
    }
    if (!this.data.password.trim()) {
      app.toast('请输入密码')
      return
    }
    // loading('登录中')
    this.setData({
      disabled: true,
      signing: true
    })
    _signin(this.data.name, this.data.password).then(res => {
      if (res.data.IsSuccess) {
        let { 
          Name: name,
          Tel: tel,
          City: city,
          DepartMent: depart,
          ID: id
        } = res.data.Data
        app.store.userinfo = {
          name,
          tel,
          city,
          depart
        }
        wx.setStorageSync('memberid', id)
        wx.showModal({
          title: '温馨提示',
          content: res.data.Msg,
          showCancel: false,
          success: r => {
            if (r.confirm) {
              this.setData({
                disabled: false,
                signing: false
              }, () => {
                let pages = getCurrentPages()
                if (pages.length > 1) {
                  wx.navigateBack()
                } else {
                  wx.redirectTo({
                    url: '/pages/index/index'
                  })
                }
              })
            }
          }
        })
      }
      else
      {
        app.toast(res.data.Msg)
        this.setData({
          disabled: false,
          signing: false
        })
      }
    }).catch(err => {
      console.log(err)
      wx.showModal({
        title: '对不起',
        content: '请求失败，请稍后再试',
        showCancel: false
      })
      this.setData({
        disabled: false,
        signing: false
      })
    })
  },
  onLoad (options) {},
  onReady () {},
  onShow () {},
  onHide () {},
  onUnload () {},
  onPullDownRefresh () {},
  onReachBottom () {},
  onShareAppMessage () {}
})